﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zero.Core.Domain;
using Zero.POCO.Compagne;
using Zero.POCO.Enum.Helper.Support;
using Zero.POCO.Helper.Support;
using Zero.POCO.Interfaces;
using Zero.POCO.Support;
using Zero.Presenter.Donnees;
using TypeMedia = Zero.POCO.Compagne.TypeMedia;

namespace Zero.Presenter
{


    public class PlanPresenter:IPlanPresenter
    {
        private IPlanView _planView;
        private IRepository<PlanTV, Guid> _repositoryPlanTV;
        private IRepository<PlanRadio, Guid> _repositoryPlanRadio;
        private IRepository<PlanPresse, Guid> _repositoryPlanPresse;
        private IReadOnlyRepository<SupportPresse, int> _supportPrRepository;
        private IReadOnlyRepository<SupportRadio, int> _supportRdRepository;
        private IReadOnlyRepository<SupportTV, int> _supportTvRepository;
        private IRepository<TarifTv, Guid> _tariftvRepository;
        private IRepository<TarifRd, Guid> _tarifrdRepository;
        private IRepository<TarifPr, Guid> _tarifPrRepository;
        public PlanPresenter(IPlanView planView, IRepositoryPlan<PlanTV, Guid> repositoryPlanTV, IRepositoryPlan<PlanRadio, Guid> repositoryPlanRadio, IRepositoryPlan<PlanPresse, Guid> repositoryPresse, 
            IReadOnlyRepository<SupportPresse, int> supportPrRepository, IReadOnlyRepository<SupportRadio, int> supportRdRepository, IReadOnlyRepository<SupportTV, int> supportTvRepository, IRepository<TarifTv,Guid> tariftvRepository,IRepository<TarifRd,Guid> tarifrdRepository,IRepository<TarifPr,Guid> tarifPrRepository)
        {
            _planView = planView;
            _repositoryPlanRadio = repositoryPlanRadio;
            _repositoryPlanTV = repositoryPlanTV;
            _repositoryPlanPresse = repositoryPresse;
            _supportPrRepository = supportPrRepository;
            _supportRdRepository = supportRdRepository;
            _supportTvRepository = supportTvRepository;
            _tarifPrRepository = tarifPrRepository;
            _tarifrdRepository = tarifrdRepository;
            _tariftvRepository = tariftvRepository;

        }
        public void Display()
        {
            _planView.SupportPrs = _supportPrRepository.FindAll();
            _planView.SupportRds = _supportRdRepository.FindAll();
            _planView.SupportTvs = _supportTvRepository.FindAll();
            _planView.TarifRds = _tarifrdRepository.FindAll().ToList();
            _planView.TarifTvs = _tariftvRepository.FindAll().ToList();
            _planView.TarifPrs = _tarifPrRepository.FindAll().ToList();

            var modelTransmis=new List<ModelViewPlan>();
            var planTvs = _repositoryPlanTV.FindAll();
            foreach (var planTv in planTvs)
            {
                var Mvplan = new ModelViewPlan
                {
                    Nom = planTv.Name,
                    NombreInsertion = planTv.NombreSignalitique,
                    TypeMedia = TypeMedia.TV,
                    id = planTv.id,
                   
                };
                Mvplan.MapSupportTarifs = new List<ModelViewMapTarif>();
                if (planTv.ListeMapSupportTarifs!=null)
                foreach (var listeMapSupportTarif in planTv.ListeMapSupportTarifs)
                {
                    var model = new ModelViewMapTarif();
                    model.Support=new ModelSupport();
                    SupportTV supportTvRepositoryFindBy = _supportTvRepository.FindBy(listeMapSupportTarif.SupportId);
                    model.Support.Name = supportTvRepositoryFindBy.Libelle;
                    model.Support.id = supportTvRepositoryFindBy.id;
                    model.GrilleTarifaire = listeMapSupportTarif.TarifTv.nom;
                    model.GrilleTv = listeMapSupportTarif.TarifTv;
                    model.id = listeMapSupportTarif.id;
                    Mvplan.MapSupportTarifs.Add(model);
                }
                 
                modelTransmis.Add(Mvplan);
            }

            var planRds = _repositoryPlanRadio.FindAll();
            foreach (var planRd in planRds)
            {
                var Mvplan = new ModelViewPlan
                {
                    Nom = planRd.Name,
                    NombreInsertion = planRd.NombreSignalitique,
                    TypeMedia = TypeMedia.RD,
                    id = planRd.id,
                };
                Mvplan.MapSupportTarifs = new List<ModelViewMapTarif>();
                if (planRd.ListeMapSupportTarifs != null)
                foreach (var ListeMapSupportTarif in planRd.ListeMapSupportTarifs)
                {
                    var model = new ModelViewMapTarif();

                    model.Support = new ModelSupport();
                    SupportRadio supportRdRepositoryFindBy = _supportRdRepository.FindBy(ListeMapSupportTarif.SupportId);
                    model.Support.Name = supportRdRepositoryFindBy.Libelle;
                    model.Support.id = supportRdRepositoryFindBy.id;
                    model.GrilleTarifaire = ListeMapSupportTarif.TarifRd.nom;
                    model.GrilleRd = ListeMapSupportTarif.TarifRd;
                    model.id = ListeMapSupportTarif.id;
                    Mvplan.MapSupportTarifs.Add(model);
                }


                modelTransmis.Add(Mvplan);
            }


            var planPrs = _repositoryPlanPresse.FindAll();
            foreach (var planPr in planPrs)
            {
                var Mvplan = new ModelViewPlan
                {
                    Nom = planPr.Name,
                    NombreInsertion = planPr.NombreSignalitique,
                    TypeMedia = TypeMedia.PR,
                    id = planPr.id,
                };

                Mvplan.MapSupportTarifs = new List<ModelViewMapTarif>();
                if (planPr.ListeMapSupportTarifs != null)
                foreach (var ListeMapSupportTarif in planPr.ListeMapSupportTarifs)
                {
                    var model = new ModelViewMapTarif();

                    model.Support = new ModelSupport();
                    model.id = ListeMapSupportTarif.id;
                    SupportPresse supportPrRepositoryFindBy = _supportPrRepository.FindBy(ListeMapSupportTarif.SupportId);
                    model.Support.Name = supportPrRepositoryFindBy.Libelle;
                    model.Support.id = supportPrRepositoryFindBy.id;
                    model.GrilleTarifaire = ListeMapSupportTarif.TarifPr.nom;
                    model.Grillepr = ListeMapSupportTarif.TarifPr;
                    model.id = ListeMapSupportTarif.id;
                    Mvplan.MapSupportTarifs.Add(model);
                }
                
                modelTransmis.Add(Mvplan);
            }

            _planView.ModelViewPlans = modelTransmis;
        }

        public void WriteChanges(ModelViewPlan modelViewPlan)
        {
            switch (modelViewPlan.TypeMedia)
            {
                case TypeMedia.TV:
                {
                    var plantv = new PlanTV()
                    {
                        id = modelViewPlan.id,
                        Name = modelViewPlan.Nom
                    };
                    if (modelViewPlan.MapSupportTarifs!=null)
                        foreach (var MapSupportTarif in modelViewPlan.MapSupportTarifs)
                        {
                            var mapsuptar = new MapSupportTarif();
                            mapsuptar.id = MapSupportTarif.id;
                            mapsuptar.SupportId = MapSupportTarif.Support.id;
                            mapsuptar.TarifTv = MapSupportTarif.GrilleTv;
                            if(plantv.ListeMapSupportTarifs==null) plantv.ListeMapSupportTarifs=new List<MapSupportTarif>();
                            plantv.ListeMapSupportTarifs.Add(mapsuptar);
                        }
                    _repositoryPlanTV.Save(plantv);
                    break;
                }
                case TypeMedia.RD:
                {
                    var planrd = new PlanRadio()
                    {
                        id = modelViewPlan.id,
                        Name = modelViewPlan.Nom
                    };
                    if (modelViewPlan.MapSupportTarifs != null)
                        foreach (var MapSupportTarif in modelViewPlan.MapSupportTarifs)
                        {
                            var mapsuptar = new MapSupportTarif();
                            mapsuptar.id = MapSupportTarif.id;
                            mapsuptar.SupportId = MapSupportTarif.Support.id;
                            mapsuptar.TarifRd = MapSupportTarif.GrilleRd;
                            if (planrd.ListeMapSupportTarifs == null) planrd.ListeMapSupportTarifs = new List<MapSupportTarif>();
                            planrd.ListeMapSupportTarifs.Add(mapsuptar);
                        }
                    _repositoryPlanRadio.Save(planrd);
                    break;
                }
                case TypeMedia.PR:
                {
                    var planpr = new PlanPresse()
                    {
                        id = modelViewPlan.id,
                        Name = modelViewPlan.Nom
                    };
                    if (modelViewPlan.MapSupportTarifs != null)
                        foreach (var MapSupportTarif in modelViewPlan.MapSupportTarifs)
                        {
                            var mapsuptar = new MapSupportTarif();
                            mapsuptar.id = MapSupportTarif.id;
                            mapsuptar.SupportId = MapSupportTarif.Support.id;
                            mapsuptar.TarifPr = MapSupportTarif.Grillepr;

                            if (planpr.ListeMapSupportTarifs == null) planpr.ListeMapSupportTarifs = new List<MapSupportTarif>();
                            planpr.ListeMapSupportTarifs.Add(mapsuptar);
                        }
                    _repositoryPlanPresse.Save(planpr);
                    break;
                }
            }
        }
    }

    public interface IPlanView
    {
        List<ModelViewPlan> ModelViewPlans { get; set; }
        List<TarifTv> TarifTvs { get; set; }
        List<TarifRd> TarifRds { get; set; }
        List<TarifPr> TarifPrs { get; set; }

        IEnumerable<SupportTV> SupportTvs { get; set; }
        IEnumerable<SupportRadio> SupportRds { get; set; }
        IEnumerable<SupportPresse> SupportPrs { get; set; }
    }

    public interface IPlanPresenter
    {
        void Display();
        void WriteChanges(ModelViewPlan modelViewPlan);
    }


    public class ModelViewPlan
    {
       
        public string Nom { get; set; }
        public TypeMedia TypeMedia { get; set; }
        public int NombreInsertion { get; set; }
        public List<ModelViewMapTarif> MapSupportTarifs { get; set; }
        public Guid id { get; set; }
    }


    public class ModelViewMapTarif
    {
        public Guid id { get; set; }
        public ModelSupport Support { get; set; }
        public String GrilleTarifaire { get; set; }
        public TarifTv GrilleTv { get; set; }

        public TarifRd GrilleRd { get; set; }
        public TarifPr Grillepr { get; set; }

    }

    public class ModelSupport
    {
        public String Name { get; set; }
        public int id { get; set; }

    }

}
